package coop.bancocredicoop.proyectos.gd.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.classic.Session;

import coop.bancocredicoop.proyectos.gd.Pager;
import coop.bancocredicoop.proyectos.gd.ServletUtils;
import coop.bancocredicoop.proyectos.gd.query.DataService;
import coop.bancocredicoop.proyectos.gd.reports.DocumentTypesReport;
import coop.bancocredicoop.proyectos.gd.reports.QueryResult;

public class TiposDeDocumentoIndexServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private static final String tiposDeDocumento = "/jsp/tiposDeDocumento/index.jsp";
	private static final Log log = LogFactory.getLog(TiposDeDocumentoIndexServlet.class);
	
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int limit = 50;
		int offset = ServletUtils.getParameterAsInteger(request, "offset");
		
		DataService dataService = new DataService();
		Session session = dataService.getSession();
		QueryResult documentTypesResult = null;
		DocumentTypesReport documentTypesReport = new DocumentTypesReport();
			
		try {
			
			documentTypesResult = documentTypesReport.getAll(session, offset, limit);
			
			Pager pager = new Pager(offset, limit, documentTypesResult.getTotalItems());
			
			request.setAttribute("items", documentTypesResult.getItems());			
			request.setAttribute("pager", pager);
			
		} catch (Exception e) {
			log.error("Error in DocumentTypesReport", e);
		} finally {
			dataService.closeSession(session);
		}
		
		getServletContext().getRequestDispatcher(tiposDeDocumento).forward(request, response);
		
    }
	
}
